<template>
  <div class="container">
    <div class="app-container">
      <!-- expand-on-click-node 设值树形组件点击树形 点击图标才折叠 -->
      <el-tree :expand-on-click-node="false" :data="depts" :props="defaultProps" default-expand-all>
        <!-- 插槽展示数据使得数据不重复 -->
        <!-- v-slot="{ node, data }" 只能作用在template -->
        <template v-slot="{ data }">
          <el-row style="width: 100%;height: 40px" type="flex" justify="space-between" align="middle">
            <el-col>
              {{ data.name }}
            </el-col>
            <el-col :span="4">
              <span class="tree-manager">{{ data.managerName }}</span>
              <!-- 下拉菜单组件 -->
              <!-- 点击添加子部门传递的id -->
              <el-dropdown @command="operateDept($event, data.id)">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <!-- ref绑定子组件实例 可以调用子组件方法; 子组件传递数据；.sync可以接收子组件传递给父组件的事件或者变量；父组件监听updata事件-->
    <add-dept
      ref="addDept"
      :show-dialog.sync="showDialog"
      :current-node-id="currentNodeId"
      @updateDepartment="getDepartment"
    />
  </div>
</template>
<script>
import { getDepartment, delDepartment } from '@/api/department'
import { transListToTreeData } from '@/utils/index'
import addDept from './components/addDept.vue'
export default {
  name: 'Department',
  components: { addDept },
  data() {
    return {
      // 声明一个响应式数据记录id 默认为null
      currentNodeId: null,
      // 弹窗默认关闭
      showDialog: false,
      depts: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created() {
    // 调用
    this.getDepartment()
  },
  methods: {
    // 封装成异步方法
    async getDepartment() {
      const result = await getDepartment()
      this.depts = transListToTreeData(result, 0)
    },
    // 下拉菜单指令方法
    operateDept(type, id) {
      if (type === 'add') {
        this.showDialog = true
        this.currentNodeId = id // 记录当前点击的id
      } else if (type === 'edit') {
        this.showDialog = true
        this.currentNodeId = id
        // 更新prop是异步动作 渲染组件是同步的
        // 父组件调用子组件方法获取数据
        // 使用$nextTick是因为弹出层后紧接着调用子组件方法，第一次弹出dialog时子组件还未渲染完，等showDialog这个属性设置的数据完成渲染之后再调用子组件方法
        this.$nextTick(() => {
          this.$refs.addDept.getDepartmentDetail()
        })
      } else {
        // !!!!!!!!!!!删除有问题
        this.$confirm('您确认要删除该部门吗', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        }).then(async() => {
          await delDepartment(id)
          // 提示消息
          this.$message.success('删除部门成功')
          // 调用方法重置树形组件中的部门数据
          this.getDepartment()
        }).catch(() => {
          this.$message('取消输入')
        })
      }
    }
  }
}
</script>
<style scoped lang="scss">
.app-container {
  padding: 30px 120px;
  font-size: 14px;

  .tree-manager {
    width: 80px;
    margin-right: 10px;
    display: inline-block;
  }
}
</style>
